package com.syaccp.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageInfo;
import com.syaccp.model.SysDept;
import com.syaccp.model.SysDictData;
import com.syaccp.model.*;
import com.syaccp.page.Page;
import com.syaccp.request.SysDeptRequest;
import com.syaccp.request.SysUserRequest;
import com.syaccp.request.TOrderDetailRequest;
import com.syaccp.response.OrderResponse;
import com.syaccp.response.TOrderResponse;
import com.syaccp.service.*;
import com.syaccp.utils.Result;
import com.syaccp.utils.SecurityUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;


/**
 * <p>
 * 部门表 前端控制器
 * </p>
 *
 * @author lixin
 * @since 2023-07-02
 */
@Api(tags = "部门")
@RestController
@RequestMapping("/api/dept")
public class DeptController {

    @Resource
    private DeptService deptService;


    @Autowired
    private SysDeptService sysDeptService;

    @Autowired
    private DictDataService dictDataService;

    @Resource
    private OrderDetailService orderDetailService;

    @Autowired
    private RoleService roleService;




    /**
     * 查询所有审核评级
     * */
    @GetMapping("/zpshType")
    public Result queryZpshType(){
        Result r = new Result();
        r.setCode(200);
        r.setMessage("查询成功");
        r.setData(dictDataService.queryAllByType());
        return r;
    }

    /**
     * 素材等级统计  柱状图
     * */
    @GetMapping("/querySumDetail")
    public Result querySumDetail(){
        Result r = new Result();
        SysDept dept = new SysDept();
        dept.setParentId(100);
        List<SysDept> sysDepts = sysDeptService.queryAllById(dept);
        List<SysDictData> sysDictDatas = dictDataService.queryAllByType();
        List<ArrayList> list = new ArrayList();
        for (int i = 0; i < sysDictDatas.size(); i++) {
            ArrayList<Integer> arrayList = new ArrayList<>();
            list.add(arrayList);
            SysDictData sysDictData = sysDictDatas.get(i);
            for (int j = 0; j < sysDepts.size(); j++) {
                SysDept sysDept = sysDepts.get(j);
                int i1 = orderDetailService.queryAllByT(sysDept.getName(), sysDictData.getLabel());
                list.get(i).add(i1);
            }
        }
        r.setCode(200);
        r.setData(list);
        r.setMessage("查询成功");
        return r;
    }




    /**
     * 素材评价统计  柱状图
     * */
    @GetMapping("/sumComment")
    public  Result querysumComment(){
        Result r = new Result();
        SysDept dept = new SysDept();
        dept.setParentId(100);
        List<SysDept> sysDepts = sysDeptService.queryAllById(dept);
        List<SysDictData> sysDictDatas = dictDataService.queryAllByCommentType();
        List list = new ArrayList();
        String sum = "";
        String sum1 = "";

        for (int i = 0; i < sysDepts.size(); i++) {
            SysDept sysDept = sysDepts.get(i);
            for (int j = 0; j < sysDictDatas.size(); j++) {
                SysDictData sysDictData = sysDictDatas.get(j);
                int i1 = orderDetailService.queryAllByCommentNumInt(sysDept.getName(), sysDictData.getLabel());
                if(j == sysDictDatas.size()-1){
                    sum1 += "\""+sysDictData.getLabel()+"\":\""+i1+"\"";
                }else{
                    sum1 += "\""+sysDictData.getLabel()+"\":\""+i1+"\",";
                }
            }
            sum = "{\"product\":\""+sysDept.getName()+"\","+sum1+"}";
            sum1 = "";
            list.add(sum);
        }
        r.setCode(200);
        r.setData(list);
        r.setMessage("查询成功");
        return r;
    }


    /**
     * 角色
     * */
    @GetMapping("/queryRole")
    public Result queryRole(){
        Result result = new Result();
        List<SysRole> list = roleService.list();
        if(list!=null){
            result.setCode(200);
            result.setData(list);
            result.setMessage("查询成功");
        }else{
            result.setCode(200);
            result.setMessage("查无数据");
        }
        return  result;
    }


    /**
     * 列表
     * */
    @GetMapping("/queryAll")
    public synchronized Result queryAll(@ModelAttribute TOrderDetailRequest tOrderDetailRequest){

        Result r = new Result();
        int i =  orderDetailService.queryAllCount(tOrderDetailRequest);
        Page page = new Page();
        page.setPageSize(20);
        page.setTotalCount(i);
        if(i>0){
            if(tOrderDetailRequest.getIndex() >= page.getTotalPageCount()){
                page.setCurrPageNo(page.getTotalPageCount());
                tOrderDetailRequest.setIndex(page.getCurrPageNo());
            } else if (tOrderDetailRequest.getIndex() <=0) {
                page.setCurrPageNo(1);
                tOrderDetailRequest.setIndex(1);
            }else{
                page.setCurrPageNo(tOrderDetailRequest.getIndex());
                tOrderDetailRequest.setIndex(tOrderDetailRequest.getIndex());
            }
            List<TOrderDetail> tOrderDetails = orderDetailService.queryAll(tOrderDetailRequest);

            if(tOrderDetails == null ){
                r.setCode(500);
                r.setMessage("查无数据");
            }else{
                page.setGoodsList(tOrderDetails);
                r.setCode(200);
                r.setData(page);
                r.setMessage("查询成功");
            }
        }else{
            r.setCode(500);
            r.setMessage("查无数据");
        }
        return r;
    }


    /**
     * 风格类型  圆环图
     * */
    @GetMapping("/querySumOrder")
    public Result querySumOrder(){
        Result r = new Result();
        SysDept dept = new SysDept();
        dept.setParentId(100);
        List<SysDept> sysDepts = sysDeptService.queryAllById(dept);
        List<SysDictData> sysDictDatas = dictDataService.queryAllProduct();
        List list = new ArrayList();

        for (int i = 0; i < sysDepts.size(); i++) {
            SysDept sysDept = sysDepts.get(i);
            OrderResponse orderResponse = new OrderResponse();
            List<TOrderResponse> tOrderDetails = new ArrayList<>();
            tOrderDetails.clear();
            orderResponse.setName(sysDept.getName());
            for (int j = 0; j < sysDictDatas.size(); j++) {
                SysDictData sysDictData = sysDictDatas.get(j);
                TOrderResponse tOrderResponse  = new TOrderResponse();

                int i1 = orderDetailService.queryAllByOrder(sysDept.getName(),sysDictData.getLabel());
                tOrderResponse.setName(sysDictData.getLabel());
                tOrderResponse.setValue(i1);
                tOrderDetails.add(tOrderResponse);
            }
            orderResponse.setList(tOrderDetails);
            list.add(orderResponse);
        }
        r.setCode(200);
        r.setData(list);
        r.setMessage("查询成功");
        return r;
    }




    /**
     * 查询所有风格
     * */
    @GetMapping("/productType")
    public Result queryProductType(){
        Result r = new Result();
        r.setCode(200);
        r.setMessage("查询成功");
        r.setData(dictDataService.queryAllProduct());
        return r;
    }

    /**
     * 查询所有评论
     * */
    @GetMapping("/commentType")
    public  Result queryCommentType(){
        Result r = new Result();
        r.setCode(200);
        r.setMessage("查询成功");
        r.setData(dictDataService.queryAllByCommentType());
        return r;
    }

    /**
     * 所有公司
     * */
    @GetMapping("/sysDept")
    public  Result query(){
        Result r = new Result();
        SysDept dept = new SysDept();
        dept.setParentId(100);
        r.setCode(200);
        r.setData(sysDeptService.queryAllById(dept));
        r.setMessage("查询成功");
        return r;
    }


    @ApiOperation(value="根据登录用户查询部门信息，用户信息不需要传递过来")
    @GetMapping("/selectDeptAll")
    public Result selectDeptAll(){
        SysUser sysUser= SecurityUtil.getLoginUser();
        return deptService.selectDeptListByUser(sysUser);
    }

    @GetMapping("/deptList")
    public Result deptList(){
        List<SysDeptRequest>list=deptService.getAllDepartments();
        return Result.success(list);
    }

    @PostMapping("/pageGetDept")
    @ResponseBody
    public Result pageGetDept(int index){
        PageInfo<SysDeptRequest> pageInfo=deptService.pageGetDept(index);
        System.out.println(pageInfo);
        return Result.success(pageInfo);
    }

    @PutMapping("/updateDept")
    @ResponseBody
    public Result updateDept(@RequestBody SysDeptRequest sysDeptRequest){
        int num=deptService.updateDept(sysDeptRequest);
        return Result.success(num);
    }


    @ApiOperation(value = "获取制作公司列表")
    @RequestMapping(value = "/getCompanyInfo", method = RequestMethod.GET)
    public Result<List<SysDept>> queryCompanyInfo() {
        return Result.success(deptService.queryCompanyInfo());
    }
}

